/**
 * Created by Jone on 2016/7/14.
 */
var express = require('express');
var router = express.Router();
var Project = require('../models/project_model.js').Project;
var ProjectUserSchema = require('../models/project_user_model.js').ProjectUser;
var User = require('../models/user_model.js').User;

/* GET home page. */

router.get('/', function (req, res) {
    switch(req.query.type) {
        case 'participated':
            //执行代码块 1
            ProjectUserSchema.find({userId : req.query.userId})
                .populate('projectId')
                .exec(function(err, docs) {
                    var projects = [];
                    docs.forEach(function(doc) {
                        var project = doc.projectId;
                        projects.push(project);
                    });
                    res.json({code: 0, msg: 'success', data: projects});
                });
            break;
        case 'created':
            Project.find({userId: req.query.userId}, function (err, docs) {
                res.json({code: 0, msg: 'success', data: docs});
            });
            break;
        default:
            ProjectUserSchema.find({userId : req.query.userId})
                .populate('projectId')
                .exec(function(err, projectUsers) {
                    //查找自己创建的项目
                    Project.find({userId: req.query.userId}, function (err, projects) {
                        //添加参加的项目
                        projectUsers.forEach(function(projectUser) {
                            var project = projectUser.projectId;
                            projects.push(project);
                        });
                        res.json({code: 0, msg: 'success', data: projects});
                    });
                });

    }
});

router.get('/:id', function (req, res) {
    Project.findById(req.params.id, function (err, doc) {
        res.json({code: 0, msg: 'success', data: doc});
    });
});

// 查询项目成员
router.get('/:id/users', function (req, res) {
    console.log("请求参数：" + JSON.stringify(req.params));
    ProjectUserSchema.find({projectId : req.params.id})
        .populate('userId')
        .exec(function(err, docs) {
            res.json({code: 0, msg: 'success', data: docs});
        });
});

// 添加项目成员
router.post('/:id/users', function (req, res) {
    User.findOne({email : req.body.email}, function (err, user) {
        if(user == null || user == undefined) {
            res.json({code: 1, msg: '用户不存在！'});
        } else {
            var member = {};
            member.userId = user._id;
            member.projectId = req.body.projectId;
            ProjectUserSchema.findOne(member, function (err, doc) {
                if (doc) {
                    console.log('保存失败,已存在该成员');
                    res.json({code: 1, msg: '保存失败,已存在该成员'});
                } else {
                    ProjectUserSchema.create(member, function (err) {
                        if (err) {
                            console.log('保存失败');
                            res.json({code: 1, msg: '保存失败！'});
                        } else {
                            console.log('保存成功');
                            res.json({code: 0, msg: '保存成功！'});
                        }
                    });
                }
            });



        }
    });
});

router.post('/', function (req, res) {
    Project.create(req.body, function (err) {
        if (err) {
            console.log('保存失败');
        } else {
            console.log('保存成功');
        }
    });
    var restult = {code: 0, msg: '保存成功！'};
    res.json(restult);
});

router.put('/', function (req, res) {
    Project.findByIdAndUpdate(req.body._id, {$set : req.body}, function (err) {
        if (err) {
            console.log('保存失败');
        } else {
            console.log('保存成功');
        }
    });
    var restult = {code: 0, msg: '修改成功！', data: req.body};
    res.json(restult);
});

router.delete('/:id', function (req, res) {
    Project.remove({"_id" : req.params.id}, function (err, docs) {
        res.json({code: 0, msg: 'success', data: docs});
    });
});


module.exports = router;